﻿using LibraryInterface;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Linq;
using System.IO;

namespace Test.TransactionAndProcedureFunctionality
{
    [TestClass()]
    public class LibraryInterfaceTest
    {
        [TestMethod()]
        [DeploymentItem("LibraryInterface.exe")]
        public void TransactionUnsubscribingTest()
        {
            int personID = 3;
            int libraryID = 1;
            LibraryEntities lbEntity = new LibraryEntities();
            LibrariesSubscriber subscription = new LibrariesSubscriber();
            subscription.PersonID = personID;
            subscription.LibraryID = libraryID;
            try
            {
                lbEntity.LibrariesSubscribers.AddObject(subscription);
                lbEntity.SaveChanges();
                LibraryInterface_Accessor.TransactionUnsubscribing(personID, libraryID);
                bool actual = lbEntity.LibrariesSubscribers.Any(s => s.PersonID == personID && s.LibraryID == libraryID);
                bool expected = false;
                Assert.AreEqual(expected, actual);
            }
            catch (Exception ex)
            {
                lbEntity.LibrariesSubscribers.DeleteObject(subscription);
            }
        }

        [TestMethod()]
        [DeploymentItem("LibraryInterface.exe")]
        public void TransactionUnsubscribingWithBooksTest()
        {
            int personID = 3;
            int libraryID = 1;
            LibraryEntities lbEntity = new LibraryEntities();
            LibrariesSubscriber subscription = new LibrariesSubscriber();
            subscription.PersonID = personID;
            subscription.LibraryID = libraryID;
            subscription.ListID = 1;
            StringWriter text = new StringWriter();
            Console.SetOut(text);
            try
            {
                lbEntity.LibrariesSubscribers.AddObject(subscription);
                lbEntity.SaveChanges();
                LibraryInterface_Accessor.TransactionUnsubscribing(personID, libraryID);
                string actualText = text.ToString();
                string expectedText = "Can`t unsubscribe untill all books are returned!\r\n";
                Assert.AreEqual(expectedText, actualText);
            }
            finally
            {
                lbEntity.LibrariesSubscribers.DeleteObject(subscription);
                lbEntity.SaveChanges();
            }
        }

        [TestMethod()]
        [DeploymentItem("LibraryInterface.exe")]
        public void StoredProcedureUnSubscribingTest()
        {
            int personID = 3;
            int libraryID = 1;
            LibraryEntities lbEntity = new LibraryEntities();
            LibrariesSubscriber subscription = new LibrariesSubscriber();
            subscription.PersonID = personID;
            subscription.LibraryID = libraryID;
            try
            {
                lbEntity.LibrariesSubscribers.AddObject(subscription);
                lbEntity.SaveChanges();
                LibraryInterface_Accessor.StoredProcedureUnSubscribing(personID, libraryID);
                bool actual = lbEntity.LibrariesSubscribers.Any(s => s.PersonID == personID && s.LibraryID == libraryID);
                bool expected = false;
                Assert.AreEqual(expected, actual);
            }
            catch (Exception ex)
            {
                lbEntity.LibrariesSubscribers.DeleteObject(subscription);
            };
        }

        [TestMethod()]
        [DeploymentItem("LibraryInterface.exe")]
        public void StoredProcedureSubscribingTest()
        {
            LibraryEntities lbEntity = new LibraryEntities();
            int personID = 2;
            int libraryID = 1;

            try
            {
                LibraryInterface_Accessor.StoredProcedureSubscribing(personID, libraryID);
                bool actual = lbEntity.LibrariesSubscribers.Any(s => s.PersonID == personID && s.LibraryID == libraryID);
                bool expected = true;
                Assert.AreEqual(expected, actual);
            }
            finally
            {
                LibrariesSubscriber subscription = new LibrariesSubscriber();
                subscription = lbEntity.LibrariesSubscribers.First(s => s.PersonID == personID && s.LibraryID == libraryID);
                lbEntity.LibrariesSubscribers.DeleteObject(subscription);
                lbEntity.SaveChanges();
            }
        }
    }
}
